﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutList.cshtml";
}

<div class="admin-main">
    <style type="text/css">
        .ul_incr li {
            width: 60px;
            font-size: 16px;
            line-height: 28px;
            cursor: pointer;
            text-align: center;
            flex: auto;
            /*white-space:nowrap;*/
        }

        /*.ul_incr {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;//1px solid #CCC;
        }*/

        .popUpMenu {
            padding: 2px 5px;
            position: absolute;
            color: #FFFFFF;
            border: 2px solid #CCC;
            border-radius: 5px;
            background: #405467;
            z-Index: 999;
            width: 65px;
            display:none;

        }
    </style>

    <blockquote class="layui-elem-quote p10">
        <div id="pop" class="popUpMenu">
            <ul class="ul_incr">
                <li><a>亚马逊</a></li>
                <li><a>敦煌</a></li>
            </ul>
        </div>

        <form id="formSearch" class="layui-form">
            <div class="layui-form-text">             
                <label class="layui-form-label">产品类目：</label>
                <div class="layui-input-inline" style="width:10%">
                    @Html.CategoryHtml(ViewData["BaseCategory"],"NZ")
                </div>
                <div class="layui-input-inline">
                    @Html.SearchBtnHtml("查询")
                </div>

                <div class="layui-input-inline" style="float:right;">
                    <a class="layui-btn" lay-filter="submitForm" id="saveForm">一键闪存</a>
                </div>
                <div class="layui-input-inline" style="float:right;width:90px">
                    <a class="layui-btn" lay-filter="submitForm" id="move">引用到</a>                    
                </div>
                <div style="float:right;">
                    @Html.TopToolBarHtml(ViewData["ActionFormRightTop"])
                </div>
            </div>
        </form>
    </blockquote>
    <blockquote class="layui-elem-quote p10">
        <form id="formEdit" class="layui-form">
            <div class="layui-field-box">
                <table id="defaultTable" lay-filter="defaultruv"></table>
                <script type="text/html" id="bar">
                    @Html.ToolBarHtml(ViewData["ActionList"])
                </script>
            </div>
        </form>
    </blockquote>
</div>
<script type="text/javascript">
    layui.use(['table', 'common', 'form','tablePlug', 'optimizeSelectOption'], function () {
        var table = layui.table,
            form = layui.form,
            common = layui.common,
             $ = layui.$;
        @*var selectItemDic =@Html.SelectOptionsData(ViewData["OptionsData_En"]); 
        var getOptions = function (field) {
            return function (data) {
                var value = data[field];
                if (selectItemDic[field]) {
                    var options = [];
                    $.each(selectItemDic[field].split('^'),function(idx,item){
                        options.push('<option value="'+ item +'" ' + (value === item ? 'selected' : '') + '>'+item+'</option>');
                    });
                }
                return [
                  '<select name="city" lay-filter="city_select" lay-verify=\"required\" lay-search="" value="' + value + '">',
                  '<option value="" >请选择或搜索</option>',
                  ,options.join('')
                  ,'</select>'
                ].join('');
            
            };
        };*@

        //表格
        table.render({
            id: 'defaultReload'
           , elem: '#defaultTable'
           , height: 680
           , url: '/DataSrv/Product/List?ProductType=2' //数据接口
           , page: true //开启分页
           , cols: [[ //表头
                     { checkbox: true, fixed: false }
                     , { title: 'Image', field: 'ImageUrl',  width: '7%', templet: '<div><a target="_blank" href="{{d.OriProductUrl}}"><img prodId="{{d.Id}}" style="width: 50px; height:50px; display: block;" src="{{d.ImageUrl}}"/></a></div>', align: 'center' }
                     , { field: 'SkuId', width: '7%', title: 'Sku', align: 'center' }
                     , { field: 'Title', title: 'Title', width: '40%', align: 'center', templet: '<div>{{d.Title}}</div>' }
                     //, { field: 'Title', title: 'Title', width: '40%', align: 'center', templet: '<div><input type="text" lay-verify="required" value="{{d.Title}}" lay-vertype="tips" id="prop_1" class="layui-input" autocomplete="off"></div>' }
                    , { field: 'UpdateOn', title: '更新时间', width: '15%', templet: '<div>{{d.UpdateOn}}</div>', align: 'center' }
                       @*@Html.TabaleHeadHtml(ViewData["TableHead"],true)*@
                     , { field: '', title: '操作',toolbar: "#bar" }
           ]]
          , where: { CategoryCode: $("#selCategoryCode").val() }
        });
        var active = {
            reload: function () {
                $('#ajax-loader').fadeOut(); //todo：加载滚动条
                //执行重载
                table.reload('defaultReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                  , where: urlToJson(encodeURIComponent($("#formSearch").serialize()))
                });
            }
        };
        //服务器排序
        table.on('sort(defaultruv)', function (obj) {
            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，如：
            table.reload('defaultReload', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。 layui 2.1.1 新增参数
              , where: { //请求参数
                  field: obj.field //排序字段
                , order: obj.type //排序方式
              }
            });
        });
        $('#btnSearch').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('#saveForm').on('click', function () {
            //check必填项
            if (!layui.form.checkInput($('#formEdit'))) return false;

            var productList = [];
            var productRows = $(".layui-table-main tr");
            if (productRows != null) {
                for (var i = 0; i < productRows.length; i++) {
                    var productData = { KeyAttributes: "" };
                    var inputs = $(productRows[i]).find('td input');

                    for (var j = 1; j < inputs.length; j++) {
                        var propVal = $(inputs[j])[0].value;
                        var endStr = j == inputs.length - 1 ? "" : "^";
                        productData.KeyAttributes += propVal + endStr;
                    }
                    productData.Id = $(productRows[i]).find('td img').attr("prodId");
                    productList.push(productData);
                }

                $.ajax({
                    url: "/Datasrv/Product/updateProductBatch?categoryCode=" + $("#selCategoryCode").val(),
                    type: "POST",
                    data: { productParam: productList },
                    dataType: "json",
                    success: function (data) {
                        if (data.state == "success") {
                            common.msgSuccess("更新成功");
                        } else {
                            common.msgError("更新失败");
                        }
                    }
                });
            }

        });

        $("#saveForm").hover(function() {
            openMsg();
        }, function() {
            layer.close(subtips);
        });
        function openMsg() {
            subtips = layer.tips('提示信息', '#sub',{tips:[1,'#ff6700'],time: 30000});
        } 
        
        //add
        $('#btnAdd').on('click', function () {
            common.openTop({
                title: '操作添加', w: '670px', h: '500px', content: '/Datasrv/Product/Add/'
            });
        });

        //$("tr icon-add")
        //监听工具条
        table.on('tool(defaultruv)', function (obj) {
            var data = obj.data;
            var categporyCode = $("#selCategoryCode").val();
            switch (obj.event) {
                case "move":
                    //敦煌
                    $.ajax({
                        url: "/Datasrv/Finish/AddDhGateMeta",
                        type: "POST",
                        data: { "productId": data.Id},
                        dataType: "json",                        
                        success: function (data) {
                            if (data.state == "success") {
                                common.msgSuccess("引用成功");
                            } else {
                                common.msgError("引用失败");
                            }
                        }
                    });

                    break;
                case "detail":
                    //show(obj);
                    $.ajax({
                        url: "/Datasrv/Finish/AddAmazonMeta",//AddAmazonMeta    AddDhGateMeta
                        type: "POST",
                        data: { "productId": data.Id},
                        dataType: "json",                        
                        success: function (data) {
                            if (data.state == "success") {
                                common.msgSuccess("引用成功");
                            } else {
                                common.msgError("引用失败");
                            }
                        }
                    });

                    break;
                case "del":
                    layer.confirm('确定要删除吗？', function (index) {
                        $.ajax({
                            url: "/Datasrv/Product/Delete",
                            type: "POST",
                            data: { "Id": data.Id },
                            dataType: "json",
                            success: function (data) {
                                if (data.state == "success") {
                                    obj.del();//删除这一行
                                    layer.close(index); //关闭弹框
                                    common.msgSuccess("删除成功");
                                } else {
                                    common.msgError("删除失败");
                                }
                            }
                        });
                    });
                    break;
                case "edit":
                    common.openTop({
                        title: '编辑子商品', w: '1020px', h: '700px', content: '/Datasrv/SubProduct/Edit/' + data.Id + '?isEng=1'
                    });
                    //common.openTop({
                    //    detail: true,
                    //    title: '编辑子商品', w: '1400px', h: '750px', content: '/Datasrv/SubProduct/Edit/' + data.Id, clickOK: function (index) {
                    //        common.close(index);
                    //    }
                    //});
                    break;

                    //window.open('/Datasrv/Product/Edit/' + data.Id, "_blank");
                    //location.href = '/Datasrv/Product/Edit/' + data.Id;
       
                default:break;
            }
        });

        function show(event){
            //console.log(divId);
            $(".showbox").hide();
            var left = 600;//event.parentElement.parentElement.parentElement.offsetLeft+(event.parentElement.parentElement.offsetWidth-30)/2-50;
            var top = 500;//event.parentElement.parentElement.parentElement.offsetTop + 48;
            var showbox=$('#pop').css({
                top:'50%',
                left:'50%',
            }).addClass("showbox");

            $('#pop').attr("colIndex",$(event).attr("index"));
            $('#pop').show();
        }
    });
</script>


